﻿using System;
using WickedFlame.Core.Shapes;

namespace WickedFlame.Core.State
{
    [Serializable]
    public class ImageMemento : ShapeMemento
    {
        double x;
        double y;
        double width;
        double height;
        byte[] buffer;

        public override void SaveProperties(VectorShape shape)
        {
            base.SaveProperties(shape);

            ImageShape r = shape as ImageShape;
            if (r != null)
            {
                x = r.Rectangle.X;
                y = r.Rectangle.Y;
                width = r.Rectangle.Width;
                height = r.Rectangle.Height;
                buffer = r.BufferFromImage(r.Image);
            }
        }

        public override void ResetProperties(VectorShape shape)
        {
            base.ResetProperties(shape);

            ImageShape s = shape as ImageShape;
            if (s != null)
            {
                s.Rectangle = new System.Windows.Rect(
                    x, y, width, height);

                s.Image = s.ImageFromBuffer(buffer);
            }
        }

        public override VectorShape GenerateShape()
        {
            var shape = new ImageShape();
            shape.ResetState(this);

            return shape;
        }
    }
}
